export function getScreenScale() {
    const designDraftWidth = 3840;//设计稿的宽度
    const designDraftHeight = 2160;//设计稿的高度
    const design = designDraftWidth / designDraftHeight;
    let clientWidth = document.documentElement.clientWidth;
    let clientHeight = document.documentElement.clientHeight;
    let client = clientWidth / clientHeight;
    //根据屏幕的变化适配的比例
    const scale = client < design ? (clientWidth / designDraftWidth) : (clientHeight / designDraftHeight);
    //缩放比例
    return scale;
}
/**
 * 防抖函数，默认延迟500ms后触发下一次函数的执行。
 * @param callback 回调函数
 * @param delay 防抖延迟时间,单位毫秒，默认为500毫秒
 * @returns 
 */
export function debounce(callback, delay = 500) {
    let timer = null;
    let flag = true;
    return function (...args) {
        clearTimeout(timer);
        if (flag) {
            callback.apply(null, args);
            flag = false;
        }
        timer = setTimeout(function () {
            flag = true;
        }, delay);
    }
}
/**
 * 节流函数，默认每隔500ms函数执行一次
 * @param callback 回调函数
 * @param rate 函数执行频率，单位毫秒，默认500毫秒
 * @returns 
 */
export function throttle(callback, rate = 500) {
    let lastTime = 0;
    let flag = true;
    return function (...args) {
        if (Date.now() - lastTime > rate) {
            lastTime = Date.now();
            flag = true;
        }
        if (flag) {
            callback.apply(null, args);
            flag = false;
        }
    }
}
//获取屏幕分享权限
export function openScreenCapture() {
    if (navigator.getDisplayMedia) {
        return navigator.getDisplayMedia({
            video: true
        });
    } else if (navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia) {
        return navigator.mediaDevices.getDisplayMedia({
            video: true,
            audio: true
        });
    } else if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
        return navigator.mediaDevices.getUserMedia({
            video: { mediaSource: 'screen' }
        });
    } else {
        console.log('The current browser does not support screen sharing!');
    }
}
/**
 * 录制屏幕
 */
export function recordScreen(fileName = 'video.webm') {
    openScreenCapture().then(stream => {
        console.log(stream);
        const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")
            ? "video/webm; codecs=vp9"
            : "video/webm"
        let mediaRecorder = new MediaRecorder(stream, {
            mimeType: mime
        })

        let chunks = []
        mediaRecorder.addEventListener('dataavailable', function (e) {
            chunks.push(e.data)
        })

        mediaRecorder.addEventListener('stop', function () {
            let blob = new Blob(chunks, {
                type: chunks[0].type
            })
            let url = URL.createObjectURL(blob)
            let a = document.createElement('a')
            a.href = url
            a.download = fileName;
            a.click()
        })
        // 必须手动启动
        mediaRecorder.start();
    });
}
// 下载文件
export function fileDownload(downloadUrl, fileName) {
    let aLink = document.createElement("a");
    aLink.style.display = "none";
    // 以防base64过长导致下载失败，所以将base64转成blob格式
    aLink.href = URL.createObjectURL(dataURIToBlob(downloadUrl));
    aLink.download = fileName;
    document.body.appendChild(aLink);
    aLink.click();
    document.body.removeChild(aLink);
};

// base64转blob方法
export function dataURIToBlob(dataURI) {
    let binStr = atob(dataURI.split(",")[1]),
        len = binStr.length,
        arr = new Uint8Array(len);

    for (var i = 0; i < len; i++) {
        arr[i] = binStr.charCodeAt(i);
    }
    return new Blob([arr]);
};
/**
 * 格式化提示的数字，默认保留一位小数
 * @param {*} value 数字
 * @param {*} fixed 小数位数
 * @returns 
 */
export function formatTipVlaue(value, fixed = 1) {
    if (!value || value == 0) {
        return 0;
    }
    return (value).toFixed(fixed);
}
/**
 * 每隔interval步执行一次请求
 * @param {*} args 
 * @param {*} interval 
 * @param {*} callback 
 */
export function setIntervalExc(args, callback, interval = 2) {
    if (args['step'] == 0 || args['step'] % interval == 0 || args['step'] == args['totalSteps'] - 1) {
        callback && callback();
    }
};